package com.nattakute.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository;

import com.nattakute.dao.ComplaintDAO;
import com.nattakute.model.Complaint;

@Repository
public class ComplaintDAOImpl implements ComplaintDAO{

	@Autowired
	private SessionFactory sessionFactory;
	
	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}


	@SuppressWarnings("unchecked")
	@Cacheable("complaints")
	public List<Complaint> getComplaint() {
		// TODO Auto-generated method stub
		return sessionFactory.getCurrentSession().createQuery("from Complaint").list();
	}

	
	@SuppressWarnings("unchecked")
	@Cacheable("complaint")
	public Complaint getComplaintById(Integer id) {
		List<Complaint> complaints = new ArrayList<Complaint>();
		try {
			complaints = sessionFactory.getCurrentSession().createQuery("from Complaint where complaintID =:id").setInteger("id", id).list();
			return complaints.get(0);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@CacheEvict(value="complaints", allEntries=true)
	public Boolean addComplaint(Complaint complaint) {
		try {
			sessionFactory.getCurrentSession().save(complaint);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	@CacheEvict(value="complaints", allEntries=true)
	public Boolean deleteComplaint(Complaint complaint) {
		try {
			sessionFactory.getCurrentSession().delete(complaint);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return false;
	}

	
	public Boolean updateComplaint(Complaint complaint) {
		try {
			sessionFactory.getCurrentSession().update(complaint);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return false;
	}

	@SuppressWarnings("unchecked")
	
	public List<Complaint> getListCompaintByUser(int userID) {
		List<Complaint> listCompaints = new ArrayList<Complaint>();
		
		listCompaints = sessionFactory.getCurrentSession().createQuery("from Complaint where userID=:userID").setInteger("userID", userID).list();
		
//		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Complaint.class);
//		Criteria criteriaUser = criteria.createCriteria("userID");
//		criteriaUser.add(Restrictions.eq("userID", userID));
//		try {
//			listCompaints = criteriaUser.list();
//			if(listCompaints == null){
//				return null;
//			}			
//		} catch (Exception e) {
//			return null;
//		}
		return listCompaints;
	}

	
	@SuppressWarnings("unchecked")
	public List<Complaint> getListComplaintBySolveUser(int userID) {
		List<Complaint> listCompaints = new ArrayList<Complaint>();
		
		listCompaints = sessionFactory.getCurrentSession().createQuery("from Complaint where userSolve =:userID and statusID =:statusID").setInteger("userID", userID).setInteger("statusID", 2).list();
		
//		try {			
//			Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Complaint.class).add(Restrictions.eq("userSolve", userID)).add(Restrictions.eq("statusID", 2));;
//			
////			criteria.createAlias("complaint.users", "u");
////			criteria.createAlias("complaint.status", "s");
////			criteria.add(Restrictions.eq("u.userID", userID));
////			criteria.add(Restrictions.eq("s.statusID", 2));
//			
////			Criteria criteriaStatus = criteria.createCriteria("statusID");
////			criteria.add(Restrictions.eq("userSolve", userID)).add(Restrictions.eq("statusID", 2));
////			criteriaStatus.add(Restrictions.eq("statusID", 2));
//			listCompaints = criteria.list();
//			if(listCompaints == null){
//				return null;
//			}			
//		} catch (Exception e) {
//			return null;
//		}
		return listCompaints;
	}
	

}
